//
//  SelectThemeStyleView.m
//  LingQian
//
//  Created by xieyingze on 2018/1/22.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "SelectThemeStyleView.h"
#import "ThemeStyleCollectionCell.h"

@interface SelectThemeStyleView()<UICollectionViewDelegate,UICollectionViewDataSource>{
    CGFloat _H;
}
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) BaseButton *backView;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation SelectThemeStyleView

#pragma mark - init

- (void)initData {
    _H = 240;
}

- (void)initSubViews {
    [self addSubview:self.backView];
    [self.backView addSubview:self.mainView];
    [self.mainView addSubview:self.collectionView];
}

- (void)autoLayoutSubViews {
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@(_H));
        make.bottom.equalTo(@(_H));
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(40, 0, 0, 0));
    }];
}

#pragma mark - method


#pragma mark - Interface

- (void)showSelectThemeStyleView {
    self.collectionView.hidden = YES;
    self.backgroundColor = [kCOLOR_black colorWithAlphaComponent:0.01];
    [kUI_Window addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    @weakify(self);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        @strongify(self);
        [self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(@0);
        }];
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:10 initialSpringVelocity:2 options:UIViewAnimationOptionCurveLinear animations:^{
            self.backgroundColor = [kCOLOR_black colorWithAlphaComponent:0.3];
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {
            [self.collectionView reloadData];
        }];
    });
}

- (void)hideSelectThemeStyleView {
    [self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(_H));
    }];
    @weakify(self);
    [UIView animateWithDuration:0.25 animations:^{
        @strongify(self);
        self.backgroundColor = [kCOLOR_black colorWithAlphaComponent:0.01];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ThemeStyleCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThemeStyleCollectionCell" forIndexPath:indexPath];
    cell.indexPath = indexPath;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    self.collectionView.hidden = NO;
    cell.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, _H);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((indexPath.row/3.0+0.5) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0 options:UIViewAnimationOptionTransitionNone animations:^{
            cell.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
        } completion:^(BOOL finished) {
            
        }];
    });
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0:{
            [ThemeManager shareInstance].themeStyle = kThemeStyleGray;
        }
            break;
        case 1:{
            [ThemeManager shareInstance].themeStyle = kThemeStyleOrange;
        }
            break;
        case 2:{
            [ThemeManager shareInstance].themeStyle = kThemeStyleRed;
        }
            break;
        default:
            break;
    }
    if (self.selectCompleted) {
        self.selectCompleted(indexPath.row);
    }
    [self hideSelectThemeStyleView];
}

#pragma makr - Event


#pragma mark - Setter


#pragma mark - Getter

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = kCOLOR_white;
    }
    return _mainView;
}

- (BaseButton *)backView {
    if (!_backView) {
        _backView = [BaseButton new];
        _backView.backgroundColor = kCOLOR_clear;
        [_backView addTarget:self action:@selector(hideSelectThemeStyleView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        CGFloat itemH = _H - 40;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(itemH * 414/736.0, itemH * 0.9);
        layout.minimumInteritemSpacing = 0.f;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kUI_WIDTH,itemH) collectionViewLayout:layout];
        _collectionView.contentInset = UIEdgeInsetsMake(10, 10 , 10, 10);
        _collectionView.backgroundColor = kCOLOR_white;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollsToTop = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[ThemeStyleCollectionCell class] forCellWithReuseIdentifier:@"ThemeStyleCollectionCell"];
    }
    return _collectionView;
}

@end
